package gui;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;



public class ScreenManager extends Canvas
{
    // STAŁE OKREŚLAJĄCE KODY PRZYCISKÓW ---------------------------------------
    
    public static final int LEFT_KEY = - LEFT;
    public static final int RIGHT_KEY = - RIGHT;
    public static final int UP_KEY = - UP;
    public static final int DOWN_KEY = - DOWN;
    public static final int FIRE_KEY = - FIRE;
    
    public static final int LEFT_SOFTKEY = -6;
    public static final int RIGHT_SOFTKEY = -7;
    public static final int CLEAR_KEY = -8;
    
    public static final int NUM0_KEY = KEY_NUM0;
    public static final int NUM1_KEY = KEY_NUM1;
    public static final int NUM2_KEY = KEY_NUM2;
    public static final int NUM3_KEY = KEY_NUM3;
    public static final int NUM4_KEY = KEY_NUM4;
    public static final int NUM5_KEY = KEY_NUM5;
    public static final int NUM6_KEY = KEY_NUM6;
    public static final int NUM7_KEY = KEY_NUM7;
    public static final int NUM8_KEY = KEY_NUM8;
    public static final int NUM9_KEY = KEY_NUM9;
    
    public static final int POUND_KEY = KEY_POUND;
    public static final int STAR_KEY = KEY_STAR;
    // -------------------------------------------------------------------------
    private static ScreenManager manager = null;
    
    
    protected static MIDlet midlet = null;
    
    private AbstractScreen currentScreen = null;
    private Theme currentTheme = null;
    
    private boolean fullScreen = false;
    private boolean ruler = false; 
    
    private boolean leftMenuKeyPressed = false;
    private boolean rightMenuKeyPressed = false;
    private ScreenManager()
    {
        setFullScreenMode(true);
        // utworzenie domyślnego tematu
        currentTheme = new Theme();
    }
    
    public static ScreenManager init(MIDlet m)
    {
        if(manager != null)
            throw new IllegalStateException("ScreenManager został już przygotowany.");
        midlet = m;
        manager = new ScreenManager();
        return manager;
    }
    
    public static ScreenManager get()
    {
        if(manager == null)
            throw new IllegalStateException("ScreenManager nie został przygotowany. Wywołaj init()");
        return manager;
    }
    
    /**
     * Przejmuje kontrole nad aplikacją.
     */
    public void takeOver()
    {
        System.out.println("taking over!!!!");
        Display.getDisplay(midlet).setCurrent(manager);
    }
    
    public final void setTheme(Theme theme)
    {
        if(theme == null)
            throw new IllegalArgumentException("Tematu nie można ustawić na null.");
        currentTheme = theme;
    }
    
    public final Theme getTheme()
    {
        return currentTheme;
    }
    
    public void setCurrentScreen(AbstractScreen screen)
    {
        if(currentScreen != null)
            currentScreen.hideNotify();
        currentScreen = screen;
        currentScreen.showNotify();
        if(ruler)
            repaint();
    }
    
    public AbstractScreen getCurrentScreen()
    {
        return currentScreen;
    }
    
    public void setFullScreen(boolean full)
    {
        fullScreen = full;
    }
    
    public boolean isFullScreen()
    {
        return fullScreen;
    }
        
    public int getDeviceWidth()
    {
        return getWidth();
    }
    
    public int getDeviceHeight()
    {
        return getHeight();
    }
    
    public int getScreenHeight()
    {
        int height = getDeviceHeight();
        if(fullScreen == false)
            height -= currentTheme.getMenuBarHeight() + currentTheme.getTitleHeight();
        return height - 2 * currentTheme.getPadding();
    }
    
    public int getScreenWidth()
    {
        return getDeviceWidth() - 2 * currentTheme.getPadding();
    }

    protected void keyPressed(int key)
    {
        if(currentScreen == null)
            return;
        switch(key)
        {
            case LEFT_SOFTKEY:
                leftMenuKeyPressed = true;
                repaintMenuBar();
                currentScreen.onCancel();
                break;
            case RIGHT_SOFTKEY:
                rightMenuKeyPressed = true;
                repaintMenuBar();
                currentScreen.onOK();
                break;
            default:
                currentScreen.onKeyPressed(key);
                break;
        }
    }

    protected void keyReleased(int key)
    {
        if(key != LEFT_SOFTKEY && key != RIGHT_SOFTKEY)
            currentScreen.onKeyReleased(key);
        else
        {
            leftMenuKeyPressed = false;
            rightMenuKeyPressed = false;
            repaintMenuBar();
        }
    }

    protected void keyRepeated(int key)
    {
        if(key != LEFT_SOFTKEY && key != RIGHT_SOFTKEY)
            currentScreen.onKeyRepeated(key);
    }
    
    private void repaintMenuBar()
    {
        if(fullScreen)
            return;
        repaint(0, getDeviceHeight() - currentTheme.getMenuBarHeight(), getDeviceWidth(), currentTheme.getMenuBarHeight());
    }
    
    protected void paint(Graphics g)
    {
        int width = getDeviceWidth();
        int height = getDeviceHeight();
        int screenWidth = getScreenWidth();
        int screenHeight = getScreenHeight();
        
        int padding = currentTheme.getPadding();
        int menuBarHeight = currentTheme.getMenuBarHeight();
        int titleHeight = currentTheme.getTitleHeight();
        
        if(currentScreen == null)
        {
            currentTheme.drawNullScreen(g, screenWidth, screenHeight);
            return;
        }
        if(fullScreen)
        {
            currentTheme.drawScreenBackground(g, width, height);
            currentScreen.drawContent(g, width, height);
        }
        else
        {
            g.setClip(0, 0, width, titleHeight);
            currentTheme.drawTitle(g, currentScreen.getTitle(), width, titleHeight);
            
            g.translate(0, height - menuBarHeight);
            g.setClip(0, 0, width, menuBarHeight);
            currentTheme.drawMenuBar(g, 
                  currentScreen.getLeftMenuText(),
                  leftMenuKeyPressed,
                  currentScreen.getRightMenuText(),
                  rightMenuKeyPressed,
                  width, 
                  menuBarHeight);
            
            g.translate(0, menuBarHeight - height + titleHeight);
            g.setClip(0, 0, width, screenHeight + 2 * padding);
            currentTheme.drawScreenBackground(g, width, screenHeight + 2 * padding);
            g.translate(padding, padding);
            g.setClip(0, 0, screenWidth, screenHeight);
            currentScreen.drawContent(g, screenWidth, screenHeight);
        }
    }
}
